//部门
define(function() {
	$("#text_parentName").blur(function(){
		if($("#text_parentName").val()==null){
			$("#parentNameMessage").css("display","none");
		}
	});
	
	var ViewModel = function() {
		var self = this;
		// 这里是分页配置,第一个参数是查询的地址，第二个参数是分页大小，第三个参数是回调函数
		self.DataGrid = new RIT.eW.DataGridAjax(
				'/department/findAllDepartment', 20, self.jobs_process);
		self.jobs_process = function(jobs) {
			if (jobs && jobs.length && jobs.length > 0) {
				$.each(jobs, function(index, val) {
					val.checked = false;
				});
			}
			var items = ko.mapping.fromJS(jobs);
			return items();
		};

		self.filter = ko.observable({
			parentName : '',
			departmentName : "",
			level : ""
		});

		// 查询
		self.search = function() {
			var data = self.filter();
			self.filter().level = self.getLevel;
			console.log(data);
			self.DataGrid.GridParams.pageIndex(1);
			self.DataGrid.GetData(data);
		};

		// 重置
		self.reset = function(data) {
			self.filter({
				departmentName : "",
				parentName : '',
				level : ""
			})
		};


		// 添加弹出框
		self.addDepartment = function() {
			layer.open({
				type : 1,
				title : "添加客户信息",
				content : $('#pop_div_department')
			});
			self.model({
				parentName : "",
				departmentName : "",
				level : "",
				isEnd : "",
				note : ""
			})
		};
		// 关闭弹出框
		self.layerClose = function() {
			layer.close(layer.index);
			toastr.info("已关闭")
			self.search();
		};

		self.model = ko.observable({
			parentName : "",
			departmentName : "",
			level : "",
			isEnd : "",
			note : ""
		})

		// 保存
		self.submit = function() {
			var data = self.model();
			console.log(data);
			$.ajax({
				url : "department/addOrUpdateDepartment",
				type : 'post',
				contentType : 'application/json',
				data : JSON.stringify(data),
				dataType : 'json',
				cache : false,
				success : function(result) {
					if (result.status == 0) {
						toastr.success(result.data, result.msg);
						layer.close(layer.index);
						self.search();
					} else {
						toastr.error(result.msg);
					}
				}
			})
		};

		// 修改
		self.updateDepartment = function(department) {
			layer.open({
				type : 1,
				content : $('#pop_div_department')
			});
			self.model(department);
			self.model().parentName=$("#text_parentName").val();
		}
		// 删除
		self.deleteDepartment = function(department) {
			// 询问框
			layer.confirm('你确定要删除吗？', {
				btn : [ '确定', '取消' ]
			// 按钮
			}, function() {
				layer.msg('确定删除', {
					icon : 1
				});
				$.ajax({
					url : "department/deleteDepartment",
					type : 'post',
					contentType : 'application/json',
					data : JSON.stringify(department),
					dataType : 'json',
					cache : false,
					success : function(result) {
						if (result.status == 0) {
							toastr.success(result.data, result.msg);
							self.search();
						} else {
							toastr.error(result.msg);
						}
					}
				})
			}, function() {
				layer.msg('已经取消', {
					time : 1000, // 1s后自动关闭
				});
			});
		}
		self.search();
		/** zTree树* */
		/** ztree */
		var ztree = "";
		self.depclick = function() {
			$("#treeText").focus();
			$("#treeDiv").show();
			$("#text_parentName").val("");
			self.ztreeshow();
		}
		self.ztreeshow = function() {
			var tree = {
				pFunction : {
					zTree : '',
					setting : {
						view : {
							dblClickExpand : false,
							selectedMulti : false,
							showTitle : false,
							showIcon : true
						},
						check : {
							enable : false
						},
						data : {
							key : {
								name : "departmentName"
							},
							simpleData : {
								enable : true,
								idKey : "id",
								pIdKey : "pId",
								rootPId : null
							}
						},
						callback : {
							onClick : self.zTreeOnClick
						}
					}
				}
		};
		$.ajax({
			url : "/department/findZtreeObject",
			type : "post",
			dataType : "json",
			contentType : 'application/json;charset=UTF-8',
			success : function(result) {
				console.log(result);
				var zNodes = result;
				ztree = $.fn.zTree.init($("#treeDemo"), tree.pFunction.setting,
						zNodes);
				ztree.expandAll(true);
				console.log(ztree);
			}
		})
		}
		self.zTreeOnClick = function(event, treeId, treeNode) {
			var node = ztree.getSelectedNodes();
			console.log(node[0].departmentName, node[0].id);
			$("#text_parentName").val(node[0].departmentName);
			self.model().parentName=node[0].departmentName;
			$("#treeDemo").css("display","none")
		}
	};

	var model = new ViewModel();
	ko.applyBindings(model);
});